package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.pojo.vo.AddressVo;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("orderController")
public class OrderController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(CartController.class);
    @Reference(check=false ,timeout=106666)
    private UserService userService;

    @Reference(check=false ,timeout=106666)
    private OrderService orderService;

    //跳转到我的订单页面
    @RequestMapping("myOrder")
    public String myOrder(Model model){
        //获取登录用户
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",user);
        return "home-index";
    }

    //我的地址管理
    @RequestMapping("homeSettingAddress")
    public String homeSettingAddress(Model model){
        //获取登录用户
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",user);
        return "home-setting-address";
    }
    //获取地址表信息
    @RequestMapping("addressInfo")
    @ResponseBody
    public Result addressInfo(){
        try {
            logger.info("获取地址表信息方法开始{}");
            List<AddressVo> list = userService.addressInfo();
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //查询省信息
    @RequestMapping("getShengInfo")
    @ResponseBody
    public Result getShengInfo(){
        try {
            logger.info("查询省信息方法开始{}");
            List<TbProvinces> list = userService.getShengInfo();
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //查询市信息
    @RequestMapping("getCityInfo")
    @ResponseBody
    public Result getCityInfo(String pid ){
        try {
            logger.info("查询市信息方法开始{}");
            List<TbCities> list = userService.getCityInfo(pid);
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //查询县信息
    @RequestMapping("getXianInfo")
    @ResponseBody
    public Result getXianInfo(String pid ){
        try {
            logger.info("查询县信息方法开始{}");
            List<TbAreas> list = userService.getXianInfo(pid);
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //增加收货人地址信息
    @RequestMapping("insertAddressInfo")
    @ResponseBody
    public Result insertAddressInfo(TbAddress address){
        try {
            //获取登录用户
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            address.setUserId(user.getUsername());
            address.setIsDefault("0");
            logger.info("增加收货人地址信息方法开始{}");
            userService.insertAddressInfo(address);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //删除地址
    @RequestMapping("deleteInfo")
    @ResponseBody
    public Result deleteInfo(Long id){
        try {
            logger.info("删除信息方法开始{}");
            userService.deleteInfo(id);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //设置默认
    @RequestMapping("isDeafult")
    @ResponseBody
    public Result isDeafult(Long id){
        try {
            logger.info("设置默认方法开始{}");
            userService.isDeafult(id);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //提交订单
    @RequestMapping("submitOrder")
    @ResponseBody
    public Result submitOrder(Long id, String carts){
        //从结算页面获取到传过来的购物车
        List<Cart> cartList = JSON.parseArray(carts, Cart.class);
        try {
            logger.info("提交订单方法开始{}");
            //获取登录用户
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            if (user == null){
                return Result.response(ResultEnum.UNAUTH).setData("结账必须登录");
            }
            //提交并生成订单，清空购物车缓存
            orderService.submitOrder(id,cartList,user);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS).setData("结算成功");
    }

    //跳转到支付页面
    @RequestMapping("payList")
    public String payList(){
        //获取订单
        return "pay";
    }


}
